home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / SPR5 / CONVERT / SPR2GMTP.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-27  |  6.7 KB  |  255 lines

  1. uses Crt, VgaKern;
  2.  
  3. type
  4.   sprite_array =  array [0..2000] of pointer;
  5. var
  6.   Gd, Gm: Integer;
  7.  
  8.   sprites: sprite_array;
  9.   blocksize:array [0..2000] of longint;
  10.  
  11. {
  12. WGT Sprite File to GameTP (by Scott Ramsay)
  13.  
  14. Note that GameTP has its own sprite format, but it is not used here.
  15. It is almost identical except it does not store how many sprites are
  16. in the file, no palette info is kept, and you cannot have empty slots.
  17. I'm sure you could easily modify this conversion program to handle his
  18. format by taking out the palette, etc.
  19.  
  20. }
  21.  
  22.  function FileExists(FileName: String): Boolean;
  23. { Boolean function that returns True if the file exists;otherwise,
  24.    it returns False. Closes the file if it exists. }
  25. var
  26.   F: File;
  27. begin
  28.   {$I-}
  29.   Assign(F, FileName);
  30.   Reset(F);
  31.   Close(F);
  32.   {$I+}
  33.   FileExists := (IOResult = 0) and (FileName <> '');
  34.  end;  { FileExists }
  35.  
  36. procedure convertsprites(infilename,outfilename:string);
  37. var
  38. filein:Text; { 256 color sprite file }
  39. fileout:File; { converted sprite file }
  40. palette:array [1..768] of char; { 256 * 3 (RGB values) }
  41. size: longint;
  42. maxcolor,maxsprite:integer;
  43. temp:pointer;
  44. a,b,i,j,spritemade:integer;
  45. buf: array [1..13] of char;  { Used for header }
  46. x,y:integer; { Screen location }
  47. col:char;  { Pixel color }
  48. c1,c2:char;
  49. result:integer;
  50. startingsprite:integer;
  51.  
  52. begin
  53.  
  54.     { Open the files }
  55. if FileExists(infilename) then
  56.    begin
  57.     Assign(filein,infilename);
  58.     Reset(filein);
  59.     Assign(fileout,outfilename);
  60.     Rewrite(fileout,1);
  61.  
  62.     Read(filein,c1);
  63.     Read(filein,c2);
  64.     a:=256*ord(c2)+ord(c1);
  65.      { Get the version number, and change the startingsprite accordingly.
  66.      If version <= 3, maxsprite contains the maximum number of sprites
  67.      that can be stored in a file.  If version > 4, maxsprite contains
  68.      the number of the highest sprite in the file. (empty sprites at
  69.      the end are not kept in the file. }
  70.     if (a <= 3) then 
  71.       startingsprite := 1
  72.     else startingsprite := 0;    { Version 4 starts at sprite 0 }
  73.  
  74.     for i:=1 to 13 do
  75.     read(filein,buf[i]); { sprite header }
  76.  
  77.     if (buf=' Sprite File ') then { see if it is a sprite file }
  78.        begin
  79.         for i:=1 to 768 do
  80.          read(filein,palette[i]); { Read in 256 color palette }
  81.     maxcolor:=256;
  82.     blockwrite(fileout,maxcolor,2); { Write the number of colors stored in file }
  83.  
  84.         blockwrite(fileout,palette,maxcolor*3);
  85.         { write palette }
  86.  
  87.         Read(filein,c1);
  88.         Read(filein,c2);
  89.         maxsprite:=256*ord(c2)+ord(c1);  { maximum sprites in this file }
  90.     blockwrite(fileout,maxsprite,2);
  91.     for i:= startingsprite to maxsprite do
  92.            begin { load them in }
  93.             Read(filein,c1);
  94.             Read(filein,c2);
  95.             spritemade:=256*ord(c2)+ord(c1); { flag to see if sprite exists }
  96.         blockwrite(fileout,spritemade,2);
  97.         if (spritemade = 1) then
  98.            begin
  99.                 Bar(0,0,319,199,0);  { maximum sprite size }
  100.                 Read(filein,c1);
  101.                 Read(filein,c2);
  102.                 a:=256*ord(c2)+ord(c1); { width }
  103.                 Read(filein,c1);
  104.                 Read(filein,c2);
  105.                 b:=256*ord(c2)+ord(c1); { height }
  106.         blockwrite(fileout,a,2); { put width and height }
  107.         blockwrite(fileout,b,2);
  108.  
  109.         { Read in the image data. Each byte represents a color
  110.         from 0-255. Obviously converting sprites that use more colors
  111.         than the current mode allows will not work. Draw sprites using
  112.         only the first colors (eg 0-16), up to maxcolors of the mode you're using. }
  113.         for y:=0 to b-1 do
  114.           for x:=0 to a-1 do
  115.                      begin
  116.                       read(filein,col);
  117.                       Pset(x,y,ord(col));
  118.                      end;
  119.  
  120.                 size := BuffSize(a,b); { get byte size of image }
  121.         getmem(temp,size);
  122.                 if (temp = nil) then
  123.              begin
  124.                       closemode;
  125.               writeln('Error: not enough heap space in convertsprites.');
  126.               halt(1);
  127.              end;
  128.                 FastGet(0,0,a-1,b-1,temp^); { Get the image in new mode }
  129.         blockwrite(fileout,temp^,size); { Write the data in getimage format }
  130.             freemem(temp,size);
  131.            end;
  132.            end;
  133.            Close(filein);
  134.            Close(fileout);
  135.        end;
  136.     end
  137.   else
  138.      begin
  139.        closemode;
  140.        writeln('Could not open 256 color sprite file');
  141.        halt(1);
  142.      end;
  143. end;
  144.  
  145. procedure freesprites(freespr:sprite_array);
  146. var
  147. i:integer;
  148. width,height:integer;
  149. begin
  150.  
  151. for i:=0 to 2000 do
  152.   begin
  153.   if freespr[i] <> nil then
  154.     freemem(freespr[i],blocksize[i]);
  155.   end;
  156. end;
  157.  
  158.  
  159.  
  160. procedure loadsprites(infilename:string;var loadspr:sprite_array);
  161. var
  162. filein:file;   { converted color sprite file }
  163. maxcolor,maxsprite:integer;
  164. size:longint;
  165. temp: pointer;
  166. a,b,i,j,spritemade:integer;
  167. red,green,blue:byte;
  168. buf:string;
  169. pal:RGBlist;
  170. begin
  171.     { Open the files }
  172.     if FileExists(infilename) then
  173.      begin
  174.       Assign(filein,infilename);
  175.       Reset(filein,1);
  176.       blockread(filein,maxcolor,2);
  177.  
  178.       fGetColors(pal);
  179.       for i:=0 to maxcolor-1 do
  180.         begin
  181.           blockread(filein,red,1);
  182.           blockread(filein,green,1);
  183.           blockread(filein,blue,1);
  184.           SetColor(i,red,green,blue);
  185.       end;
  186.  
  187.       blockread(filein,maxsprite,2);   { maximum sprites in this file }
  188.  
  189.     for i := 0 to maxsprite-1 do { load them in }
  190.        begin
  191.         blockread(filein,spritemade,2); { flag to see if sprite exists }
  192.         if (spritemade = 1) then
  193.            begin
  194.         blockread(filein,a,2); { get width and height }
  195.         blockread(filein,b,2);
  196.  
  197.                 size:=BuffSize(a,b); { get byte size of image }
  198.                 blocksize[i]:=size;
  199.                 getmem(loadspr[i],size);
  200.         if (loadspr[i] = nil) then
  201.             begin
  202.               closemode;
  203.               writeln('Error: not enough heap space in loadsprites().');
  204.               freesprites(loadspr);
  205.               halt(1);
  206.            end;
  207.                 blockread(filein,loadspr[i]^,size);
  208.            end
  209.          else loadspr[i]:=nil;
  210.  
  211.          end;
  212.     close(filein);
  213.   end;
  214. end;
  215.  
  216.  
  217.  
  218. procedure testsprites;
  219. { Loops through 10 sprites, displaying them on the screen
  220.  using the putimage method. Press a key to go to the next sprite. }
  221. var
  222. i,j:integer;
  223. c:char;
  224. begin
  225.  
  226. for i:=0 to 10 do
  227.   begin
  228.   cls(0);
  229.  
  230.    if sprites[i] <> nil then
  231.      begin
  232.      for j:=1 to 20 do
  233.        fBitDraw(random(320),random(200),sprites[i]^);
  234.        { Put the converted image on the screen }
  235.      while not keypressed do;
  236.      c:=readkey;
  237.      end;
  238.   end;
  239. end;
  240.  
  241. begin
  242.   openmode(1);
  243.  
  244.   convertsprites('sprtconv.spr','out.spr');
  245.   loadsprites('out.spr',sprites);
  246.   testsprites;
  247.   freesprites(sprites);
  248.  
  249.  
  250.    { clean up }
  251.    Closemode;
  252.  
  253. end.
  254.  
  255.